package jee.boot.entity.system;

import com.google.common.collect.Lists;
import jee.boot.common.annotation.Column;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;

import java.util.List;

/**
 * 树节点
 */
@Table(name = "st_tree_node")
public class TreeNode extends GeneralDataEntity<TreeNode> {
    private static final long serialVersionUID = 1L;
    /**
     * 树节点的顶级节点默认层级
     */
    public static final Integer TREE_FIRST_TIER = 1;
    /**
     * 树节点的path默认分隔符
     */
    public static final String TREE_PATH_SEPARATOR = "/";
    /**
     * 父ID
     */
    @Column(name = "st_parent_id", data = {"parent.id", "parent.no", "parent.code", "parent.name", "parent.desc", "parent.type", "parent.path"})
    private TreeNode parent;//引用：ST_TREE_NODE
    /**
     * 树模型ID
     */
    @Column(name = "st_tree_model_id", data = {"treeModel.id", "treeModel.no", "treeModel.code", "treeModel.name", "treeModel.desc", "treeModel.type", "treeModel.url"})
    private TreeModel treeModel;//引用：ST_TREE_MODEL，由于有此字段的存在，此处就不再冗余记录Model了，冗余存储(原本根据ST_TREE_ASIGN_SITUATION可以间接推断出来)
    /**
     * 树定位情境ID
     */
    @Column(name = "st_tree_asign_situation_id", data = {"treeAsignSituation.id", "treeAsignSituation.no", "treeAsignSituation.code", "treeAsignSituation.name", "treeAsignSituation.desc", "treeAsignSituation.type"})
    private TreeAsignSituation treeAsignSituation;//引用：ST_TREE_ASIGN_SITUATION
    /**
     * 树ID
     */
    @Column(name = "st_tree_id", data = {"treeDefinition.id", "treeDefinition.no", "treeDefinition.code", "treeDefinition.name", "treeDefinition.desc", "treeDefinition.type"})
    private TreeDefinition treeDefinition;//引用：ST_TREE，冗余存储(原本根据ST_TREE_MODEL_ID可以间接推断出来)
    /**
     * 树节点层级
     */
    @Column(name = "n_tier")
    private Integer tier;//整型，冗余存储(原本根据ST_PARENT_ID可以间接推断出来)
    /**
     * 叶子节点标志
     */
    @Column(name = "st_leaf_flag")
    private String leafFlag;//字典：yes_no，冗余存储(原本可以间接推断出来)
    /**
     * 实体ID
     */
    @Column(name = "st_entity_id")
    private String entityId;//动态模型引用(此处省略了模型的定义，因为我们可以通过ST_TREE_MODEL_ID引用的ST_TREE_MODEL数据找到MODEL，此处无需重复定义MODEL)
    /**
     * 是否级联查询出实体数据
     */
    @Transient
    private boolean cascadeEntity=false;
    /**
     * 冗余查询出来的实体数据
     */
    @Transient
    private Object entity;
    /**
     * 层级路径
     */
    @Column(name = "st_path")
    private String path;//文本，冗余存储(原本根据ST_PARENT_ID可以间接推断出来)

    @Transient
    private List<TreeNode> children = Lists.newArrayList();

    @Transient
    private boolean fullPath = true;
    /**
     * 叶子节点的数量
     */
    @Column(ignoreAlias = true, name = "(select count(1) from st_tree_node child where child.st_parent_id=a.id)", action = Column.Action.QUERY)
    private Integer childNum = 0;
    /**
     * 优先级
     */
    @Transient
    private Integer priority;
    /**
     * 是否是权限内的数据
     *  unAuth 无权限的节点
     *  rootAuth 有权限的根节点
     *  childAuth 有权限的非根节点
     */
    @Transient
    private String hasAuth="unAuth";

    public TreeNode() {
    }

    public TreeNode(String id) {
        super(id);
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public TreeModel getTreeModel() {
        return treeModel;
    }

    public void setTreeModel(TreeModel treeModel) {
        this.treeModel = treeModel;
    }

    public TreeAsignSituation getTreeAsignSituation() {
        return treeAsignSituation;
    }

    public void setTreeAsignSituation(TreeAsignSituation treeAsignSituation) {
        this.treeAsignSituation = treeAsignSituation;
    }

    public TreeDefinition getTreeDefinition() {
        return treeDefinition;
    }

    public void setTreeDefinition(TreeDefinition treeDefinition) {
        this.treeDefinition = treeDefinition;
    }

    public Integer getTier() {
        return tier;
    }

    public void setTier(Integer tier) {
        this.tier = tier;
    }

    public String getLeafFlag() {
        return leafFlag;
    }

    public void setLeafFlag(String leafFlag) {
        this.leafFlag = leafFlag;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getEntityId() {
        return entityId;
    }

    public void setEntityId(String entityId) {
        this.entityId = entityId;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public boolean isFullPath() {
        return fullPath;
    }

    public void setFullPath(boolean fullPath) {
        this.fullPath = fullPath;
    }

    public Integer getChildNum() {
        return childNum;
    }

    public void setChildNum(Integer childNum) {
        this.childNum = childNum;
    }

    public Object getEntity() {
        return entity;
    }

    public void setEntity(Object entity) {
        this.entity = entity;
    }

    public boolean isCascadeEntity() {
        return cascadeEntity;
    }

    public void setCascadeEntity(boolean cascadeEntity) {
        this.cascadeEntity = cascadeEntity;
    }

    public String getHasAuth() {
        return hasAuth;
    }

    public void setHasAuth(String hasAuth) {
        this.hasAuth = hasAuth;
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        treeNode.preUpdate();
        System.out.println(treeNode.getDynamicUpdateSql());
        System.out.println(treeNode.getUpdateSql());
    }
}
